package cn.xiongxf.pet.service.impl;

import cn.xiongxf.basic.constant.FastDfsConstants;
import cn.xiongxf.basic.exception.BusinessException;
import cn.xiongxf.basic.service.IBaiduAiAuditService;
import cn.xiongxf.basic.service.impl.BaseServiceImpl;
import cn.xiongxf.basic.util.BaiduAiUtils;
import cn.xiongxf.basic.util.LoginContext;
import cn.xiongxf.org.domain.Employee;
import cn.xiongxf.org.mapper.EmployeeMapper;
import cn.xiongxf.pet.domain.Pet;
import cn.xiongxf.pet.domain.PetDetail;
import cn.xiongxf.pet.domain.PetOnlineAuditLog;
import cn.xiongxf.pet.mapper.PetDetailMapper;
import cn.xiongxf.pet.mapper.PetMapper;
import cn.xiongxf.pet.mapper.PetOnlineAuditLogMapper;
import cn.xiongxf.pet.service.IPetService;
import cn.xiongxf.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        // 获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            // 添加宠物详情
            petDetailMapper.save(petDetail);
        }
    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        if(pet.getPetDetail() != null){
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            // 宠物详情更新：根据宠物详情id去更新
            petDetailMapper.update(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.removePetDetailByPetId(id);
    }

    @Transactional(noRollbackFor = BusinessException.class)
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            // 1.审核
            // 审核文本
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            //Boolean textBoo = BaiduAiUtils.textCensor(pet.getName());
            // 审核图片
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());

            /*// 审核图片：多张图片resources
            Boolean imageBoo = true;
            // 图片途径是否为空
            if (StringUtils.hasLength(pet.getResources())) {
                // 判断是否有多张图片
                if (pet.getResources().contains(",")) {
                    String[] resources = pet.getResources().split(",");
                    for (String resource : resources) {
                        String petResources = FastDfsConstants.DFS_SERVER_URL_PREFIX + resource;
                        imageBoo = BaiduAiUtils.imgCensor(petResources);
                        if (!imageBoo) {
                            break;
                        }
                    }
                } else {
                    String petResources = FastDfsConstants.DFS_SERVER_URL_PREFIX + pet.getResources();
                    imageBoo = BaiduAiUtils.imgCensor(petResources);
                }
            }*/
            if(textBoo && imageBoo){  // 审核成功
                // a.设置上架时间
                pet.setOnsaletime(new Date());
                // 修改状态：将状态由0 - 1
                pet.setState(1);
                petMapper.update(pet);

                // b.添加上架审核日志 - 审核成功的日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);

            }else{  // 审核失败
                // 添加上架审核日志 - 审核失败的日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
            }
        }
    }

    @Transactional
    @Override
    public void offsale(List<Long> ids) {
        // 修改状态：将状态由1 - 0
        petMapper.offsale(ids, new Date());
    }

}
